home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / pcl / sptmbr16.lha / construct.lisp < prev    next >
Lisp/Scheme  |  1992-12-21  |  41KB  |  1,065 lines

  1. ;;;-*-Mode:LISP; Package:(PCL LISP 1000); Base:10; Syntax:Common-lisp -*-
  2. ;;;
  3. ;;; *************************************************************************
  4. ;;; Copyright (c) 1985, 1986, 1987, 1988, 1989, 1990 Xerox Corporation.
  5. ;;; All rights reserved.
  6. ;;;
  7. ;;; Use and copying of this software and preparation of derivative works
  8. ;;; based upon this software are permitted.  Any distribution of this
  9. ;;; software or derivative works must comply with all applicable United
  10. ;;; States export control laws.
  11. ;;; 
  12. ;;; This software is made available AS IS, and Xerox Corporation makes no
  13. ;;; warranty about the software, its performance or its conformity to any
  14. ;;; specification.
  15. ;;; 
  16. ;;; Any person obtaining a copy of this software is requested to send their
  17. ;;; name and post office or electronic mail address to:
  18. ;;;   CommonLoops Coordinator
  19. ;;;   Xerox PARC
  20. ;;;   3333 Coyote Hill Rd.
  21. ;;;   Palo Alto, CA 94304
  22. ;;; (or send Arpanet mail to CommonLoops-Coordinator.pa@Xerox.arpa)
  23. ;;;
  24. ;;; Suggestions, comments and requests for improvements are also welcome.
  25. ;;; *************************************************************************
  26. ;;;
  27. ;;;
  28. ;;; This file defines the defconstructor and other make-instance optimization
  29. ;;; mechanisms.
  30. ;;; 
  31.  
  32. (in-package :pcl)
  33.  
  34. ;;;
  35. ;;; defconstructor is used to define special purpose functions which just
  36. ;;; call make-instance with a symbol as the first argument.  The semantics
  37. ;;; of defconstructor is that it is equivalent to defining a function which
  38. ;;; just calls make-instance. The purpose of defconstructor is to provide
  39. ;;; PCL with a way of noticing these calls to make-instance so that it can
  40. ;;; optimize them.  Specific ports of PCL could just have their compiler
  41. ;;; spot these calls to make-instance and then call this code.  Having the
  42. ;;; special defconstructor facility is the best we can do portably.
  43. ;;; 
  44. ;;;
  45. ;;; A call to defconstructor like:
  46. ;;;
  47. ;;;  (defconstructor make-foo foo (a b &rest r) a a :mumble b baz r)
  48. ;;;
  49. ;;; Is equivalent to a defun like:
  50. ;;;
  51. ;;;  (defun make-foo (a b &rest r)
  52. ;;;    (make-instance 'foo 'a a ':mumble b 'baz r))
  53. ;;;
  54. ;;; Calls like the following are also legal:
  55. ;;;
  56. ;;;  (defconstructor make-foo foo ())
  57. ;;;  (defconstructor make-bar bar () :x *x* :y *y*)
  58. ;;;  (defconstructor make-baz baz (a b c) a-b (list a b) b-c (list b c))
  59. ;;;
  60. ;;;
  61. ;;; The general idea of this implementation is that the expansion of the
  62. ;;; defconstructor form includes the creation of closure generators which
  63. ;;; can be called to create constructor code for the class.  The ways that
  64. ;;; a constructor can be optimized depends not only on the defconstructor
  65. ;;; form, but also on the state of the class and the generic functions in
  66. ;;; the initialization protocol.  Because of this, the determination of the
  67. ;;; form of constructor code to be used is a two part process.
  68. ;;;
  69. ;;; At compile time, make-constructor-code-generators looks at the actual
  70. ;;; defconstructor form and makes a list of appropriate constructor code
  71. ;;; generators.  All that is really taken into account here is whether
  72. ;;; any initargs are supplied in the call to make-instance, and whether
  73. ;;; any of those are constant.
  74. ;;;
  75. ;;; At constructor code generation time (see note about lazy evaluation)
  76. ;;; compute-constructor-code calls each of the constructor code generators
  77. ;;; to try to get code for this constructor.  Each generator looks at the
  78. ;;; state of the class and initialization protocol generic functions and
  79. ;;; decides whether its type of code is appropriate.  This depends on things
  80. ;;; like whether there are any applicable methods on initialize-instance,
  81. ;;; whether class slots are affected by initialization etc.
  82. ;;; 
  83. ;;;
  84. ;;; Constructor objects are funcallable instances, the protocol followed to
  85. ;;; to compute the constructor code for them is quite similar to the protocol
  86. ;;; followed to compute the discriminator code for a generic function.  When
  87. ;;; the constructor is first loaded, we install as its code a function which
  88. ;;; will compute the actual constructor code the first time it is called.
  89. ;;; 
  90. ;;; If there is an update to the class structure which might invalidate the
  91. ;;; optimized constructor, the special lazy constructor installer is put back
  92. ;;; so that it can compute the appropriate constructor when it is called.
  93. ;;; This is the same kind of lazy evaluation update strategy used elswhere
  94. ;;; in PCL.
  95. ;;;
  96. ;;; To allow for flexibility in the PCL implementation and to allow PCL users
  97. ;;; to specialize this constructor facility for their own metaclasses, there
  98. ;;; is an internal protocol followed by the code which loads and installs
  99. ;;; the constructors.  This is documented in the comments in the code.
  100. ;;;
  101. ;;; This code is also designed so that one of its levels, can be used to
  102. ;;; implement optimization of calls to make-instance which can't go through
  103. ;;; the defconstructor facility.  This has not been implemented yet, but the
  104. ;;; hooks are there.
  105. ;;;
  106. ;;;
  107.  
  108. (defmacro defconstructor
  109.       (name class lambda-list &rest initialization-arguments)
  110.   (expand-defconstructor class
  111.              name
  112.              lambda-list
  113.              (copy-list initialization-arguments)))
  114.  
  115. (defun expand-defconstructor (class-name name lambda-list supplied-initargs)
  116.   (let ((class (find-class class-name nil))
  117.     (supplied-initarg-names
  118.       (gathering1 (collecting)
  119.         (iterate ((name (*list-elements supplied-initargs :by #'cddr)))
  120.           (gather1 name)))))
  121.     (when (null class)
  122.       (error "defconstructor form being compiled (or evaluated) before~@
  123.               class ~S is defined."
  124.          class-name))
  125.     `(progn
  126.        ;; In order to avoid undefined function warnings, we want to tell
  127.        ;; the compile time environment that a function with this name and
  128.        ;; this argument list has been defined.  The portable way to do this
  129.        ;; is with defun.
  130.        (proclaim '(notinline ,name))
  131.        (defun ,name ,lambda-list
  132.      (declare (ignore ,@(extract-parameters lambda-list)))
  133.      (error "Constructor ~S not loaded." ',name))
  134.  
  135.        ,(make-top-level-form `(defconstructor ,name)
  136.                  '(load eval)
  137.       `(load-constructor
  138.          ',class-name
  139.          ',(class-name (class-of class))
  140.          ',name
  141.          ',supplied-initarg-names
  142.          ;; make-constructor-code-generators is called to return a list
  143.          ;; of constructor code generators.  The actual interpretation
  144.          ;; of this list is left to compute-constructor-code, but the
  145.          ;; general idea is that it should be an plist where the keys
  146.          ;; name a kind of constructor code and the values are generator
  147.          ;; functions which return the actual constructor code.  The
  148.          ;; constructor code is usually a closures over the arguments
  149.          ;; to the generator.
  150.          ,(make-constructor-code-generators class
  151.                         name
  152.                         lambda-list
  153.                         supplied-initarg-names
  154.                         supplied-initargs))))))
  155.  
  156. (defun load-constructor (class-name metaclass-name constructor-name
  157.              supplied-initarg-names code-generators)
  158.   (let ((class (find-class class-name nil)))
  159.     (cond ((null class)
  160.        (error "defconstructor form being loaded (or evaluated) before~@
  161.                    class ~S is defined."
  162.           class-name))
  163.       ((neq (class-name (class-of class)) metaclass-name)
  164.        (error "When defconstructor ~S was compiled, the metaclass of the~@
  165.                    class ~S was ~S.  The metaclass is now ~S.~@
  166.                    The constructor must be recompiled."
  167.           constructor-name
  168.           class-name
  169.           metaclass-name
  170.           (class-name (class-of class))))
  171.       (t
  172.        (load-constructor-internal class
  173.                       constructor-name
  174.                       supplied-initarg-names
  175.                       code-generators)
  176.        constructor-name))))
  177.  
  178. ;;;
  179. ;;; The actual constructor objects.
  180. ;;; 
  181. (defclass constructor ()               
  182.      ((class                    ;The class with which this
  183.     :initarg :class                ;constructor is associated.
  184.     :reader constructor-class)        ;The actual class object,
  185.                         ;not the class name.
  186.                         ;      
  187.       (name                    ;The name of this constructor.
  188.     :initform nil                ;This is the symbol in whose
  189.     :initarg :name                ;function cell the constructor
  190.     :reader constructor-name)        ;usually sits.  Of course, this
  191.                         ;is optional.  defconstructor
  192.                         ;makes named constructors, but
  193.                         ;it is possible to manipulate
  194.                         ;anonymous constructors also.
  195.                         ;
  196.       (code-type                ;The type of code currently in
  197.     :initform nil                ;use by this constructor.  This
  198.     :accessor constructor-code-type)    ;is mostly for debugging and
  199.                         ;analysis purposes.
  200.                         ;The lazy installer sets this
  201.                         ;to LAZY.  The most basic and
  202.                         ;least optimized type of code
  203.                         ;is called FALLBACK.
  204.                         ;
  205.       (supplied-initarg-names            ;The names of the initargs this
  206.     :initarg :supplied-initarg-names    ;constructor supplies when it
  207.     :reader                    ;"calls" make-instance.
  208.        constructor-supplied-initarg-names)    ;
  209.                         ;
  210.       (code-generators                ;Generators for the different
  211.     :initarg :code-generators        ;types of code this constructor
  212.     :reader constructor-code-generators))    ;could use.
  213.   (:metaclass funcallable-standard-class))
  214.  
  215.  
  216. ;;;
  217. ;;; Because the value in the code-type slot should always correspond to the
  218. ;;; funcallable-instance-function of the constructor, this function should
  219. ;;; always be used to set the both at the same time.
  220. ;;;
  221. (defun set-constructor-code (constructor code type)
  222.   (set-funcallable-instance-function constructor code)
  223.   (set-function-name constructor (constructor-name constructor))
  224.   (setf (constructor-code-type constructor) type))
  225.  
  226.  
  227. (defmethod print-object ((constructor constructor) stream)
  228.   (printing-random-thing (constructor stream)
  229.     (format stream
  230.         "~S ~S (~S)"
  231.         (or (class-name (class-of constructor)) "Constructor")
  232.         (or (slot-value-or-default constructor 'name) "Anonymous")
  233.         (slot-value-or-default constructor 'code-type))))
  234.  
  235. (defmethod describe-object ((constructor constructor) stream)
  236.   (format stream
  237.       "~S is a constructor for the class ~S.~%~
  238.             The current code type is ~S.~%~
  239.             Other possible code types are ~S."
  240.       constructor (constructor-class constructor)
  241.       (constructor-code-type constructor)
  242.       (gathering1 (collecting)
  243.         (doplist (key val) (constructor-code-generators constructor)
  244.           (gather1 key)))))
  245.  
  246. ;;;
  247. ;;; I am not in a hairy enough mood to make this implementation be metacircular
  248. ;;; enough that it can support a defconstructor for constructor objects.
  249. ;;; 
  250. (defun make-constructor (class name supplied-initarg-names code-generators)
  251.   (make-instance 'constructor
  252.          :class class
  253.          :name name
  254.          :supplied-initarg-names supplied-initarg-names
  255.          :code-generators code-generators))
  256.  
  257. ; This definition actually appears in std-class.lisp.
  258. ;(defmethod class-constructors ((class std-class))
  259. ;  (with-slots (plist) class (getf plist 'constructors)))
  260.  
  261. (defmethod add-constructor ((class slot-class)
  262.                 (constructor constructor))
  263.   (with-slots (plist) class
  264.     (pushnew constructor (getf plist 'constructors))))
  265.  
  266. (defmethod remove-constructor ((class slot-class)
  267.                    (constructor constructor))
  268.   (with-slots (plist) class
  269.     (setf (getf plist 'constructors)
  270.       (delete constructor (getf plist 'constructors)))))
  271.  
  272. (defmethod get-constructor ((class slot-class) name &optional (error-p t))
  273.   (or (dolist (c (class-constructors class))
  274.     (when (eq (constructor-name c) name) (return c)))
  275.       (if error-p
  276.       (error "Couldn't find a constructor with name ~S for class ~S."
  277.          name class)
  278.       ())))
  279.  
  280. ;;;
  281. ;;; This is called to actually load a defconstructor constructor.  It must
  282. ;;; install the lazy installer in the function cell of the constructor name,
  283. ;;; and also add this constructor to the list of constructors the class has.
  284. ;;; 
  285. (defmethod load-constructor-internal
  286.        ((class slot-class) name initargs generators)
  287.   (let ((constructor (make-constructor class name initargs generators))
  288.     (old (get-constructor class name nil)))
  289.     (when old (remove-constructor class old))
  290.     (install-lazy-constructor-installer constructor)
  291.     (add-constructor class constructor)
  292.     (setf (gdefinition name) constructor)))
  293.  
  294. (defmethod install-lazy-constructor-installer ((constructor constructor))
  295.   (let ((class (constructor-class constructor)))
  296.     (set-constructor-code constructor
  297.               #'(lambda (&rest args)
  298.                   (multiple-value-bind (code type)
  299.                   (compute-constructor-code class constructor)
  300.                 (prog1 (apply code args)
  301.                        (set-constructor-code constructor
  302.                                  code
  303.                                  type))))
  304.               'lazy)))
  305.  
  306. ;;;
  307. ;;; The interface to keeping the constructors updated.
  308. ;;;
  309. ;;; add-method and remove-method (for standard-generic-function and -method),
  310. ;;; promise to call maybe-update-constructors on the generic function and
  311. ;;; the method.
  312. ;;; 
  313. ;;; The class update code promises to call update-constructors whenever the
  314. ;;; class is changed.  That is, whenever the supers, slots or options change.
  315. ;;; If user defined classes of constructor needs to be updated in more than
  316. ;;; these circumstances, they should use the dependent updating mechanism to
  317. ;;; make sure update-constructors is called.
  318. ;;;
  319. ;;; Bootstrapping concerns force the definitions of maybe-update-constructors
  320. ;;; and update-constructors to be in the file std-class.  For clarity, they
  321. ;;; also appear below.  Be sure to keep the definition here and there in sync.
  322. ;;; 
  323. ;(defvar *initialization-generic-functions*
  324. ;     (list #'make-instance
  325. ;           #'default-initargs
  326. ;           #'allocate-instance
  327. ;           #'initialize-instance
  328. ;           #'shared-initialize))
  329. ;
  330. ;(defmethod maybe-update-constructors
  331. ;       ((generic-function generic-function)
  332. ;        (method method))
  333. ;  (when (memq generic-function *initialization-generic-functions*)
  334. ;    (labels ((recurse (class)
  335. ;           (update-constructors class)
  336. ;           (dolist (subclass (class-direct-subclasses class))
  337. ;         (recurse subclass))))
  338. ;      (when (classp (car (method-specializers method)))
  339. ;    (recurse (car (method-specializers method)))))))
  340. ;
  341. ;(defmethod update-constructors ((class slot-class))
  342. ;  (dolist (cons (class-constructors class))
  343. ;    (install-lazy-constructor-installer cons)))
  344. ;
  345. ;(defmethod update-constructors ((class class))
  346. ;  ())
  347.  
  348.  
  349.  
  350. ;;;
  351. ;;; Here is the actual smarts for making the code generators and then trying
  352. ;;; each generator to get constructor code. This extensible mechanism allows
  353. ;;; new kinds of constructor code types to be added. A programmer defining a
  354. ;;; specialization of the constructor class can either use this mechanism to
  355. ;;; define new code types, or can override this mechanism by overriding the
  356. ;;; methods on make-constructor-code-generators and compute-constructor-code.
  357. ;;;
  358. ;;; The function defined by define-constructor-code-type will receive the
  359. ;;; class object, and the 4 original arguments to defconstructor. It can
  360. ;;; return a constructor code generator, or return nil if this type of code
  361. ;;; is determined to not be appropriate after looking at the defconstructor
  362. ;;; arguments.
  363. ;;;
  364. ;;; When compute-constructor-code is called, it first performs basic checks
  365. ;;; to make sure that the basic assumptions common to all the code types are
  366. ;;; valid.  (For details see method definition).  If any of the tests fail,
  367. ;;; the fallback constructor code type is used.  If none of the tests fail,
  368. ;;; the constructor code generators are called in order.  They receive 5
  369. ;;; arguments:
  370. ;;;
  371. ;;;   CLASS        the class the constructor is making instances of
  372. ;;;   WRAPPER      that class's wrapper
  373. ;;;   DEFAULTS     the result of calling class-default-initargs on class
  374. ;;;   INITIALIZE   the applicable methods on initialize-instance
  375. ;;;   SHARED       the applicable methosd on shared-initialize
  376. ;;;
  377. ;;; The first code generator to return code is used.  The code generators are
  378. ;;; called in reverse order of definition, so define-constructor-code-type
  379. ;;; forms which define better code should appear after ones that define less
  380. ;;; good code.  The fallback code type appears first.  Note that redefining a
  381. ;;; code type does not change its position in the list.  To do that,  define
  382. ;;; a new type at the end with the behavior.
  383. ;;; 
  384.  
  385. (defvar *constructor-code-types* ())
  386.  
  387. (defmacro define-constructor-code-type (type arglist &body body)
  388.   (let ((fn-name (intern (format nil
  389.                  "CONSTRUCTOR-CODE-GENERATOR ~A ~A"
  390.                  (package-name (symbol-package type))
  391.                  (symbol-name type))
  392.              *the-pcl-package*)))
  393.     `(progn
  394.        (defun ,fn-name ,arglist .,body)
  395.        (load-define-constructor-code-type ',type ',fn-name))))
  396.  
  397. (defun load-define-constructor-code-type (type generator)
  398.   (let ((old-entry (assq type *constructor-code-types*)))
  399.     (if old-entry 
  400.     (setf (cadr old-entry) generator)
  401.     (push (list type generator) *constructor-code-types*))
  402.     type))
  403.  
  404. (defmethod make-constructor-code-generators
  405.        ((class slot-class)
  406.         name lambda-list supplied-initarg-names supplied-initargs)
  407.   (cons 'list
  408.     (gathering1 (collecting)
  409.       (dolist (entry *constructor-code-types*)
  410.         (let ((generator
  411.             (funcall (cadr entry) class name lambda-list 
  412.                       supplied-initarg-names
  413.                       supplied-initargs)))
  414.           (when generator
  415.         (gather1 `',(car entry))
  416.         (gather1 generator)))))))
  417.  
  418. (defmethod compute-constructor-code ((class slot-class)
  419.                      (constructor constructor))
  420.   (let* ((proto (class-prototype class))
  421.      (wrapper (class-wrapper class))
  422.      (defaults (class-default-initargs class))
  423.          (make
  424.            (compute-applicable-methods (gdefinition 'make-instance) (list class)))
  425.      (supplied-initarg-names
  426.        (constructor-supplied-initarg-names constructor))
  427.          (default
  428.        (compute-applicable-methods (gdefinition 'default-initargs)
  429.                        (list class supplied-initarg-names))) ;?
  430.          (allocate
  431.            (compute-applicable-methods (gdefinition 'allocate-instance)
  432.                        (list class)))
  433.          (initialize
  434.            (compute-applicable-methods (gdefinition 'initialize-instance)
  435.                        (list proto)))
  436.          (shared
  437.            (compute-applicable-methods (gdefinition 'shared-initialize)
  438.                        (list proto t)))
  439.      (code-generators
  440.        (constructor-code-generators constructor)))
  441.     (flet ((call-code-generator (generator)
  442.          (when (null generator)
  443.            (unless (setq generator (getf code-generators 'fallback))
  444.          (error "No FALLBACK generator?")))
  445.          (funcall generator class wrapper defaults initialize shared)))
  446.       (if (or (cdr make)
  447.           (cdr default)
  448.           (cdr allocate)
  449.           (not (check-initargs-1 class
  450.                      supplied-initarg-names
  451.                      (append initialize shared)
  452.                      nil nil)))
  453.       ;; These are basic shared assumptions, if one of the
  454.       ;; has been violated, we have to resort to the fallback
  455.       ;; case.  Any of these assumptions could be moved out
  456.       ;; of here and into the individual code types if there
  457.       ;; was a need to do so.
  458.       (values (call-code-generator nil) 'fallback)
  459.       ;; Otherwise try all the generators until one produces
  460.       ;; code for us.
  461.       (doplist (type generator) code-generators
  462.         (let ((code (call-code-generator generator)))
  463.           (when code (return (values code type)))))))))
  464.  
  465. ;;;
  466. ;;; The facilities are useful for debugging, and to measure the performance
  467. ;;; boost from constructors.
  468. ;;; 
  469.  
  470. (defun map-constructors (fn)
  471.   (let ((nclasses 0)
  472.     (nconstructors 0))
  473.     (labels ((recurse (class)
  474.            (incf nclasses)
  475.            (dolist (constructor (class-constructors class))
  476.          (incf nconstructors)
  477.          (funcall fn constructor))
  478.            (dolist (subclass (class-direct-subclasses class))
  479.          (recurse subclass))))
  480.       (recurse (find-class 't))
  481.       (values nclasses nconstructors))))
  482.  
  483. (defun reset-constructors ()
  484.   (multiple-value-bind (nclass ncons)
  485.       (map-constructors #'install-lazy-constructor-installer )
  486.     (format t "~&~D classes, ~D constructors." nclass ncons)))
  487.  
  488. (defun disable-constructors ()
  489.   (multiple-value-bind (nclass ncons)
  490.       (map-constructors
  491.     #'(lambda (c)
  492.         (let ((gen (getf (constructor-code-generators c) 'fallback)))
  493.           (if (null gen)
  494.           (error "No fallback constructor for ~S." c)
  495.           (set-constructor-code c
  496.                     (funcall gen
  497.                          (constructor-class c)
  498.                          () () () ())
  499.                     'fallback)))))
  500.     (format t "~&~D classes, ~D constructors." nclass ncons)))
  501.  
  502. (defun enable-constructors ()
  503.   (reset-constructors))
  504.  
  505.  
  506. ;;;
  507. ;;; Helper functions and utilities that are shared by all of the code types
  508. ;;; and by the main compute-constructor-code method as well.
  509. ;;; 
  510.  
  511. (defvar *standard-initialize-instance-method*
  512.         (get-method #'initialize-instance
  513.             ()
  514.             (list *the-class-slot-object*)))
  515.  
  516. (defvar *standard-shared-initialize-method*
  517.         (get-method #'shared-initialize
  518.             ()
  519.             (list *the-class-slot-object* *the-class-t*)))
  520.  
  521. (defun non-pcl-initialize-instance-methods-p (methods)
  522.   (notevery #'(lambda (m) (eq m *standard-initialize-instance-method*))
  523.         methods))
  524.  
  525. (defun non-pcl-shared-initialize-methods-p (methods)
  526.   (notevery #'(lambda (m) (eq m *standard-shared-initialize-method*))
  527.         methods))
  528.  
  529. (defun non-pcl-or-after-initialize-instance-methods-p (methods)
  530.   (notevery #'(lambda (m) (or (eq m *standard-initialize-instance-method*)
  531.                   (equal '(:after) (method-qualifiers m))))
  532.         methods))
  533.  
  534. (defun non-pcl-or-after-shared-initialize-methods-p (methods)
  535.   (notevery #'(lambda (m) (or (eq m *standard-shared-initialize-method*)
  536.                   (equal '(:after) (method-qualifiers m))))
  537.         methods))
  538.  
  539. ;;;
  540. ;;; This returns two values.  The first is a vector which can be used as the
  541. ;;; initial value of the slots vector for the instance. The second is a symbol
  542. ;;; describing the initforms this class has.  
  543. ;;;
  544. ;;;  If the first value is:
  545. ;;;
  546. ;;;    :unsupplied    no slot has an initform
  547. ;;;    :constants     all slots have either a constant initform
  548. ;;;                   or no initform at all
  549. ;;;    t              there is at least one non-constant initform
  550. ;;; 
  551. (defun compute-constant-vector (class)
  552.   ;;(declare (values constants flag))
  553.   (let* ((wrapper (class-wrapper class))
  554.      (layout (wrapper-instance-slots-layout wrapper))
  555.      (flag :unsupplied)
  556.      (constants ()))
  557.     (dolist (slotd (class-slots class))
  558.       (let ((name (slot-definition-name slotd))
  559.         (initform (slot-definition-initform slotd))
  560.         (initfn (slot-definition-initfunction slotd)))
  561.     (cond ((null (memq name layout)))
  562.           ((null initfn)
  563.            (push (cons name *slot-unbound*) constants))
  564.           ((constantp initform)
  565.            (push (cons name (eval initform)) constants)
  566.            (when (eq flag ':unsupplied) (setq flag ':constants)))
  567.           (t
  568.            (push (cons name *slot-unbound*) constants)
  569.            (setq flag 't)))))
  570.     (let* ((constants-alist (sort constants #'(lambda (x y)
  571.                         (memq (car y)
  572.                               (memq (car x) layout)))))
  573.        (constants-list (mapcar #'cdr constants-alist)))
  574.     (values constants-list flag))))
  575.  
  576.  
  577. ;;;
  578. ;;; This takes a class and a list of initarg-names, and returns an alist
  579. ;;; indicating the positions of the slots those initargs may fill.  The
  580. ;;; order of the initarg-names argument is important of course, since we
  581. ;;; have to respect the rules about the leftmost initarg that fills a slot
  582. ;;; having precedence.  This function allows initarg names to appear twice
  583. ;;; in the list, it only considers the first appearance.
  584. ;;;
  585. (defun compute-initarg-positions (class initarg-names)
  586.   (let* ((layout (wrapper-instance-slots-layout (class-wrapper class)))
  587.      (positions
  588.        (gathering1 (collecting)
  589.          (iterate ((slot-name (list-elements layout))
  590.                (position (interval :from 0)))
  591.            (gather1 (cons slot-name position)))))
  592.      (slot-initargs
  593.        (mapcar #'(lambda (slotd)
  594.                (list (slot-definition-initargs slotd)
  595.                  (or (cdr (assq (slot-definition-name slotd) positions))
  596.                  ':class)))
  597.            (class-slots class))))
  598.     ;; Go through each of the initargs, and figure out what position
  599.     ;; it fills by replacing the entries in slot-initargs it fills.
  600.     (dolist (initarg initarg-names)
  601.       (dolist (slot-entry slot-initargs)
  602.     (let ((slot-initargs (car slot-entry)))
  603.       (when (and (listp slot-initargs)
  604.              (not (null slot-initargs))
  605.              (memq initarg slot-initargs))
  606.         (setf (car slot-entry) initarg)))))
  607.     (gathering1 (collecting)
  608.       (dolist (initarg initarg-names)
  609.     (let ((positions (gathering1 (collecting)
  610.                (dolist (slot-entry slot-initargs)
  611.                  (when (eq (car slot-entry) initarg)
  612.                    (gather1 (cadr slot-entry)))))))
  613.       (when positions
  614.         (gather1 (cons initarg positions))))))))
  615.  
  616.  
  617. ;;;
  618. ;;; The FALLBACK case allows anything.  This always works, and always appears
  619. ;;; as the last of the generators for a constructor.  It does a full call to
  620. ;;; make-instance.
  621. ;;;
  622.  
  623. (define-constructor-code-type fallback
  624.         (class name arglist supplied-initarg-names supplied-initargs)
  625.   (declare (ignore name supplied-initarg-names))
  626.   `(function
  627.      (lambda (&rest ignore)
  628.        (declare (ignore ignore))
  629.        (function
  630.      (lambda ,arglist
  631.        (make-instance
  632.          ',(class-name class)
  633.          ,@(gathering1 (collecting)
  634.          (iterate ((tail (*list-tails supplied-initargs :by #'cddr)))
  635.            (gather1 `',(car tail))
  636.            (gather1 (cadr tail))))))))))
  637.  
  638. ;;;
  639. ;;; The GENERAL case allows:
  640. ;;;   constant, unsupplied or non-constant initforms
  641. ;;;   constant or non-constant default initargs
  642. ;;;   supplied initargs
  643. ;;;   slot-filling initargs
  644. ;;;   :after methods on shared-initialize and initialize-instance
  645. ;;;   
  646. (define-constructor-code-type general
  647.         (class name arglist supplied-initarg-names supplied-initargs)
  648.   (declare (ignore name))
  649.   (let ((raw-allocator (raw-instance-allocator class))
  650.     (slots-fetcher (slots-fetcher class)))
  651.     `(function
  652.        (lambda (class .wrapper. defaults init shared)
  653.      (multiple-value-bind (.constants.
  654.                    .constant-initargs.
  655.                    .initfns-initargs-and-positions.
  656.                    .supplied-initarg-positions.
  657.                    .shared-initfns.
  658.                    .initfns.)
  659.          (general-generator-internal class
  660.                      defaults
  661.                      init
  662.                      shared
  663.                      ',supplied-initarg-names
  664.                      ',supplied-initargs)
  665.        .supplied-initarg-positions.
  666.        (when (and .constants.
  667.               (null (non-pcl-or-after-initialize-instance-methods-p
  668.                   init))
  669.               (null (non-pcl-or-after-shared-initialize-methods-p
  670.                   shared)))
  671.          (function
  672.            (lambda ,arglist
  673.          (declare #.*optimize-speed*)
  674.          (let* ((.instance. (,raw-allocator .wrapper. .constants.))
  675.             (.slots. (,slots-fetcher .instance.))
  676.             (.positions. .supplied-initarg-positions.)
  677.             (.initargs. .constant-initargs.))           
  678.            .positions.
  679.            
  680.            (dolist (entry .initfns-initargs-and-positions.)
  681.              (let ((val (funcall (car entry)))
  682.                (initarg (cadr entry)))
  683.                (when initarg
  684.              (push val .initargs.)
  685.              (push initarg .initargs.))
  686.                (dolist (pos (cddr entry))
  687.              (setf (%instance-ref .slots. pos) val))))
  688.  
  689.            ,@(gathering1 (collecting)
  690.                (doplist (initarg value) supplied-initargs
  691.              (unless (constantp value)
  692.                (gather1 `(let ((.value. ,value))
  693.                        (push .value. .initargs.)
  694.                        (push ',initarg .initargs.)
  695.                        (dolist (.p. (pop .positions.))
  696.                      (setf (%instance-ref .slots. .p.)
  697.                            .value.)))))))
  698.  
  699.            (dolist (fn .shared-initfns.)
  700.              (apply fn .instance. t .initargs.))
  701.            (dolist (fn .initfns.)
  702.              (apply fn .instance. .initargs.))
  703.              
  704.            .instance.)))))))))
  705.  
  706. (defun general-generator-internal
  707.        (class defaults init shared supplied-initarg-names supplied-initargs)
  708.   (flet ((bail-out () (return-from general-generator-internal nil)))
  709.     (let* ((constants (compute-constant-vector class))
  710.        (layout (wrapper-instance-slots-layout (class-wrapper class)))
  711.        (initarg-positions
  712.          (compute-initarg-positions class
  713.                     (append supplied-initarg-names
  714.                         (mapcar #'car defaults))))
  715.        (initfns-initargs-and-positions ())
  716.        (supplied-initarg-positions ())
  717.        (constant-initargs ())
  718.        (used-positions ()))
  719.                            
  720.       ;;
  721.       ;; Go through each of the supplied initargs for three reasons.
  722.       ;;
  723.       ;;   - If it fills a class slot, bail out.
  724.       ;;   - If its a constant form, fill the constant vector.
  725.       ;;   - Otherwise remember the positions no two initargs
  726.       ;;     will try to fill the same position, since compute
  727.       ;;     initarg positions already took care of that, but
  728.       ;;     we do need to know what initforms will and won't
  729.       ;;     be needed.
  730.       ;;   
  731.       (doplist (initarg val) supplied-initargs
  732.     (let ((positions (cdr (assq initarg initarg-positions))))
  733.       (cond ((memq :class positions) (bail-out))
  734.         ((constantp val)
  735.          (setq val (eval val))
  736.          (push val constant-initargs)
  737.          (push initarg constant-initargs)
  738.          (dolist (pos positions) (setf (svref constants pos) val)))
  739.         (t
  740.          (push positions supplied-initarg-positions)))
  741.       (setq used-positions (append positions used-positions))))
  742.       ;;
  743.       ;; Go through each of the default initargs, for three reasons.
  744.       ;;
  745.       ;;   - If it fills a class slot, bail out.
  746.       ;;   - If it is a constant, and it does fill a slot, put that
  747.       ;;     into the constant vector.
  748.       ;;   - If it isn't a constant, record its initfn and position.
  749.       ;;   
  750.       (dolist (default defaults)
  751.     (let* ((name (car default))
  752.            (initfn (cadr default))
  753.            (form (caddr default))
  754.            (value ())
  755.            (positions (cdr (assq name initarg-positions))))
  756.       (unless (memq name supplied-initarg-names)
  757.         (cond ((memq :class positions) (bail-out))
  758.           ((constantp form)
  759.            (setq value (eval form))
  760.            (push value constant-initargs)
  761.            (push name constant-initargs)
  762.            (dolist (pos positions)
  763.              (setf (svref constants pos) value)))
  764.           (t
  765.            (push (list* initfn name positions)
  766.              initfns-initargs-and-positions)))
  767.         (setq used-positions (append positions used-positions)))))
  768.       ;;
  769.       ;; Go through each of the slot initforms:
  770.       ;;
  771.       ;;    - If its position has already been filled, do nothing.
  772.       ;;      The initfn won't need to be called, and the slot won't
  773.       ;;      need to be touched.
  774.       ;;    - If it is a class slot, and has an initform, bail out.
  775.       ;;    - If its a constant or unsupplied, ignore it, it is
  776.       ;;      already in the constant vector.
  777.       ;;    - Otherwise, record its initfn and position
  778.       ;;
  779.       (dolist (slotd (class-slots class))
  780.     (let* ((alloc (slot-definition-allocation slotd))
  781.            (name (slot-definition-name slotd))
  782.            (form (slot-definition-initform slotd))
  783.            (initfn (slot-definition-initfunction slotd))
  784.            (position (position name layout)))
  785.       (cond ((neq alloc :instance)
  786.          (unless (null initfn)
  787.            (bail-out)))
  788.         ((member position used-positions))
  789.         ((or (constantp form)
  790.              (null initfn)))
  791.         (t
  792.          (push (list initfn nil position)
  793.                initfns-initargs-and-positions)))))
  794.  
  795.       (values constants
  796.           constant-initargs
  797.           (nreverse initfns-initargs-and-positions)
  798.           (nreverse supplied-initarg-positions)
  799.           (mapcar #'method-function
  800.               (remove *standard-shared-initialize-method* shared))
  801.           (mapcar #'method-function
  802.               (remove *standard-initialize-instance-method* init))))))
  803.  
  804.  
  805. ;;;
  806. ;;; The NO-METHODS case allows:
  807. ;;;   constant, unsupplied or non-constant initforms
  808. ;;;   constant or non-constant default initargs
  809. ;;;   supplied initargs that are arguments to constructor, or constants
  810. ;;;   slot-filling initargs
  811. ;;;
  812.  
  813. (define-constructor-code-type no-methods
  814.         (class name arglist supplied-initarg-names supplied-initargs)
  815.   (declare (ignore name))
  816.   (let ((raw-allocator (raw-instance-allocator class))
  817.     (slots-fetcher (slots-fetcher class)))
  818.     `(function
  819.        (lambda (class .wrapper. defaults init shared)
  820.      (multiple-value-bind (.constants.
  821.                    .initfns-and-positions.
  822.                    .supplied-initarg-positions.)
  823.          (no-methods-generator-internal class
  824.                         defaults
  825.                         ',supplied-initarg-names
  826.                         ',supplied-initargs)
  827.        .initfns-and-positions.
  828.        .supplied-initarg-positions.
  829.        (when (and .constants.
  830.               (null (non-pcl-initialize-instance-methods-p init))
  831.               (null (non-pcl-shared-initialize-methods-p shared)))
  832.          #'(lambda ,arglist
  833.          (declare #.*optimize-speed*)
  834.          (let* ((.instance. (,raw-allocator .wrapper. .constants.))
  835.             (.slots. (,slots-fetcher .instance.))
  836.             (.positions. .supplied-initarg-positions.))
  837.            .positions.
  838.  
  839.            (dolist (entry .initfns-and-positions.)
  840.              (let ((val (funcall (car entry))))
  841.                (dolist (pos (cdr entry))
  842.              (setf (%instance-ref .slots. pos) val))))
  843.          
  844.            ,@(gathering1 (collecting)
  845.                (doplist (initarg value) supplied-initargs
  846.              (unless (constantp value)
  847.                (gather1
  848.                  `(let ((.value. ,value))
  849.                 (dolist (.p. (pop .positions.))
  850.                   (setf (%instance-ref .slots. .p.) .value.)))))))
  851.              
  852.            .instance.))))))))
  853.  
  854. (defun no-methods-generator-internal
  855.        (class defaults supplied-initarg-names supplied-initargs)
  856.   (flet ((bail-out () (return-from no-methods-generator-internal nil)))
  857.     (let* ((constants    (compute-constant-vector class))
  858.        (layout (wrapper-instance-slots-layout (class-wrapper class)))
  859.        (initarg-positions
  860.          (compute-initarg-positions class
  861.                     (append supplied-initarg-names
  862.                         (mapcar #'car defaults))))
  863.        (initfns-and-positions ())
  864.        (supplied-initarg-positions ())
  865.        (used-positions ()))
  866.       ;;
  867.       ;; Go through each of the supplied initargs for three reasons.
  868.       ;;
  869.       ;;   - If it fills a class slot, bail out.
  870.       ;;   - If its a constant form, fill the constant vector.
  871.       ;;   - Otherwise remember the positions, no two initargs
  872.       ;;     will try to fill the same position, since compute
  873.       ;;     initarg positions already took care of that, but
  874.       ;;     we do need to know what initforms will and won't
  875.       ;;     be needed.
  876.       ;;   
  877.       (doplist (initarg val) supplied-initargs
  878.     (let ((positions (cdr (assq initarg initarg-positions))))
  879.       (cond ((memq :class positions) (bail-out))
  880.         ((constantp val)
  881.          (setq val (eval val))
  882.          (dolist (pos positions)
  883.            (setf (svref constants pos) val)))
  884.         (t
  885.          (push positions supplied-initarg-positions)))
  886.       (setq used-positions (append positions used-positions))))
  887.       ;;
  888.       ;; Go through each of the default initargs, for three reasons.
  889.       ;;
  890.       ;;   - If it fills a class slot, bail out.
  891.       ;;   - If it is a constant, and it does fill a slot, put that
  892.       ;;     into the constant vector.
  893.       ;;   - If it isn't a constant, record its initfn and position.
  894.       ;;   
  895.       (dolist (default defaults)
  896.     (let* ((name (car default))
  897.            (initfn (cadr default))
  898.            (form (caddr default))
  899.            (value ())
  900.            (positions (cdr (assq name initarg-positions))))
  901.       (unless (memq name supplied-initarg-names)
  902.         (cond ((memq :class positions) (bail-out))
  903.           ((constantp form)
  904.            (setq value (eval form))
  905.            (dolist (pos positions)
  906.              (setf (svref constants pos) value)))
  907.           (t
  908.            (push (cons initfn positions)
  909.              initfns-and-positions)))
  910.         (setq used-positions (append positions used-positions)))))
  911.       ;;
  912.       ;; Go through each of the slot initforms:
  913.       ;;
  914.       ;;    - If its position has already been filled, do nothing.
  915.       ;;      The initfn won't need to be called, and the slot won't
  916.       ;;      need to be touched.
  917.       ;;    - If it is a class slot, and has an initform, bail out.
  918.       ;;    - If its a constant or unsupplied, do nothing, we know
  919.       ;;      that it is already in the constant vector.
  920.       ;;    - Otherwise, record its initfn and position
  921.       ;;
  922.       (dolist (slotd (class-slots class))
  923.     (let* ((alloc (slot-definition-allocation slotd))
  924.            (name (slot-definition-name slotd))
  925.            (form (slot-definition-initform slotd))
  926.            (initfn (slot-definition-initfunction slotd))
  927.            (position (position name layout)))
  928.       (cond ((neq alloc :instance)
  929.          (unless (null initfn)
  930.            (bail-out)))
  931.         ((member position used-positions))
  932.         ((or (constantp form)
  933.              (null initfn)))
  934.         (t
  935.          (push (list initfn position) initfns-and-positions)))))
  936.  
  937.       (values constants
  938.           (nreverse initfns-and-positions)
  939.           (nreverse supplied-initarg-positions)))))
  940.  
  941.  
  942. ;;;
  943. ;;; The SIMPLE-SLOTS case allows:
  944. ;;;   constant or unsupplied initforms
  945. ;;;   constant default initargs
  946. ;;;   supplied initargs
  947. ;;;   slot filling initargs
  948. ;;;
  949.  
  950. (define-constructor-code-type simple-slots
  951.         (class name arglist supplied-initarg-names supplied-initargs)
  952.   (declare (ignore name))
  953.   (let ((raw-allocator (raw-instance-allocator class))
  954.     (slots-fetcher (slots-fetcher class)))
  955.     `(function
  956.        (lambda (class .wrapper. defaults init shared)
  957.      (when (and (null (non-pcl-initialize-instance-methods-p init))
  958.             (null (non-pcl-shared-initialize-methods-p shared)))
  959.        (multiple-value-bind (.constants. .supplied-initarg-positions.)
  960.            (simple-slots-generator-internal class
  961.                         defaults
  962.                         ',supplied-initarg-names
  963.                         ',supplied-initargs)
  964.          (when .constants.
  965.            (function
  966.          (lambda ,arglist
  967.            (declare #.*optimize-speed*)
  968.            (let* ((.instance. (,raw-allocator .wrapper. .constants.))
  969.               (.slots. (,slots-fetcher .instance.))
  970.               (.positions. .supplied-initarg-positions.))
  971.              .positions.
  972.          
  973.              ,@(gathering1 (collecting)
  974.              (doplist (initarg value) supplied-initargs
  975.                (unless (constantp value)
  976.                  (gather1
  977.                    `(let ((.value. ,value))
  978.                   (dolist (.p. (pop .positions.))
  979.                     (setf (%instance-ref .slots. .p.) .value.)))))))
  980.              
  981.              .instance.))))))))))
  982.  
  983. (defun simple-slots-generator-internal
  984.        (class defaults supplied-initarg-names supplied-initargs)
  985.   (flet ((bail-out () (return-from simple-slots-generator-internal nil)))
  986.     (let* ((constants (compute-constant-vector class))
  987.        (layout (wrapper-instance-slots-layout (class-wrapper class)))
  988.        (initarg-positions
  989.          (compute-initarg-positions class
  990.                     (append supplied-initarg-names
  991.                         (mapcar #'car defaults))))
  992.        (supplied-initarg-positions ())
  993.        (used-positions ()))
  994.       ;;
  995.       ;; Go through each of the supplied initargs for three reasons.
  996.       ;;
  997.       ;;   - If it fills a class slot, bail out.
  998.       ;;   - If its a constant form, fill the constant vector.
  999.       ;;   - Otherwise remember the positions, no two initargs
  1000.       ;;     will try to fill the same position, since compute
  1001.       ;;     initarg positions already took care of that, but
  1002.       ;;     we do need to know what initforms will and won't
  1003.       ;;     be needed.
  1004.       ;;   
  1005.       (doplist (initarg val) supplied-initargs
  1006.     (let ((positions (cdr (assq initarg initarg-positions))))
  1007.       (cond ((memq :class positions) (bail-out))
  1008.         ((constantp val)
  1009.          (setq val (eval val))
  1010.          (dolist (pos positions)
  1011.            (setf (svref constants pos) val)))
  1012.         (t
  1013.          (push positions supplied-initarg-positions)))
  1014.       (setq used-positions (append used-positions positions))))
  1015.       ;;
  1016.       ;; Go through each of the default initargs for three reasons.
  1017.       ;; 
  1018.       ;;   - If it isn't a constant form, bail out.
  1019.       ;;   - If it fills a class slot, bail out.
  1020.       ;;   - If it is a constant, and it does fill a slot, put that
  1021.       ;;     into the constant vector.
  1022.       ;;   
  1023.       (dolist (default defaults)
  1024.     (let* ((name (car default))
  1025.            (form (caddr default))
  1026.            (value ())
  1027.            (positions (cdr (assq name initarg-positions))))
  1028.       (unless (memq name supplied-initarg-names)
  1029.         (cond ((memq :class positions) (bail-out))
  1030.           ((not (constantp form))
  1031.            (bail-out))
  1032.           (t
  1033.            (setq value (eval form))
  1034.            (dolist (pos positions)
  1035.              (setf (svref constants pos) value)))))))
  1036.       ;;
  1037.       ;; Go through each of the slot initforms:
  1038.       ;;
  1039.       ;;    - If its position has already been filled, do nothing.
  1040.       ;;      The initfn won't need to be called, and the slot won't
  1041.       ;;      need to be touched, we are OK.
  1042.       ;;    - If it has a non-constant initform, bail-out.  This
  1043.       ;;      case doesn't handle those.
  1044.       ;;    - If it has a constant or unsupplied initform we don't
  1045.       ;;      really need to do anything, the value is in the
  1046.       ;;      constants vector.
  1047.       ;;
  1048.       (dolist (slotd (class-slots class))
  1049.     (let* ((alloc (slot-definition-allocation slotd))
  1050.            (name (slot-definition-name slotd))
  1051.            (form (slot-definition-initform slotd))
  1052.            (initfn (slot-definition-initfunction slotd))
  1053.            (position (position name layout)))
  1054.       (cond ((neq alloc :instance)
  1055.          (unless (null initfn)
  1056.            (bail-out)))
  1057.         ((member position used-positions))
  1058.         ((or (constantp form)
  1059.              (null initfn)))
  1060.         (t
  1061.          (bail-out)))))
  1062.       
  1063.       (values constants (nreverse supplied-initarg-positions)))))
  1064.  
  1065.